package studykana.adapters;

import java.util.List;

import studykana.models.Alphabet;
import toan.da.com.vn.R;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class BangChuCaiAdapter extends BaseAdapter {
	private Activity activity;
	private List<Alphabet> listAlphabet;
	private List<Integer> listIdSelected;
	private int menu;

	public BangChuCaiAdapter(List<Alphabet> listAlphabet, Activity activity,
			List<Integer> listIdSelected, int menu) {
		super();
		this.listAlphabet = listAlphabet;
		this.activity = activity;
		this.listIdSelected = listIdSelected;
		this.menu = menu;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listAlphabet.size();
	}

	@Override
	public Object getItem(int position) {
		return listAlphabet.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return listAlphabet.get(position).getId();
	}

	public int getAlphabetId(int position){
		return listAlphabet.get(position).getId();
	}

	public static class ViewHolder {
		public TextView tvMain;
		public TextView tvSub;
		public TextView tvRo;
		public CheckBox cb;
		public int id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder view;
		LayoutInflater inflator = activity.getLayoutInflater();
		if (convertView == null) {
			view = new ViewHolder();
			convertView = inflator.inflate(R.layout.bang_chu_cai_row, null);
			view.tvMain = (TextView) convertView.findViewById(R.id.tvMain);
			view.tvSub = (TextView) convertView.findViewById(R.id.tvSub);
			view.tvRo = (TextView) convertView.findViewById(R.id.tvRo);
			view.cb = (CheckBox) convertView.findViewById(R.id.checkBox);
			convertView.setTag(view);
		} else {
			view = (ViewHolder) convertView.getTag();
		}
		
		if (menu == 0) {
			view.tvMain.setText(listAlphabet.get(position).getHiragana());
			view.tvSub.setText(listAlphabet.get(position).getKatakana());
		} else {
			view.tvMain.setText(listAlphabet.get(position).getKatakana());
			view.tvSub.setText(listAlphabet.get(position).getHiragana());
		}
		view.tvRo.setText(listAlphabet.get(position).getRomaji());

		if (listIdSelected.size() != 0) {
			if (listIdSelected.contains(listAlphabet.get(position).getId())) {
				view.cb.setChecked(true);
				view.tvMain.setTextColor(Color.RED);
				view.tvSub.setTextColor(Color.RED);
				view.tvRo.setTextColor(Color.RED);
			} else {
				view.cb.setChecked(false);
				view.tvMain.setTextColor(convertView.getResources().getColor(
						R.color.TextColor));
				view.tvSub.setTextColor(convertView.getResources().getColor(
						R.color.TextColor));
				view.tvRo.setTextColor(convertView.getResources().getColor(
						R.color.TextColor));
			}
		} else {
			view.cb.setChecked(false);
			view.tvMain.setTextColor(convertView.getResources().getColor(
					R.color.TextColor));
			view.tvSub.setTextColor(convertView.getResources().getColor(
					R.color.TextColor));
			view.tvRo.setTextColor(convertView.getResources().getColor(
					R.color.TextColor));
		}

		return convertView;
	}

	public void setColorItem(View convertView) {

		new Color();
		if (!((CheckBox) convertView.findViewById(R.id.checkBox)).isChecked()) {
			((TextView) convertView.findViewById(R.id.tvMain))
					.setTextColor(Color.RED);
			((TextView) convertView.findViewById(R.id.tvSub))
					.setTextColor(Color.RED);
			((TextView) convertView.findViewById(R.id.tvRo))
					.setTextColor(Color.RED);
			((CheckBox) convertView.findViewById(R.id.checkBox))
					.setChecked(true);
		} else {
			((TextView) convertView.findViewById(R.id.tvMain))
					.setTextColor(convertView.getResources().getColor(
							R.color.TextColor));
			((TextView) convertView.findViewById(R.id.tvSub))
					.setTextColor(convertView.getResources().getColor(
							R.color.TextColor));
			((TextView) convertView.findViewById(R.id.tvRo))
					.setTextColor(convertView.getResources().getColor(
							R.color.TextColor));
			((CheckBox) convertView.findViewById(R.id.checkBox))
					.setChecked(false);
		}
	}

	public void setColorItem(View convertView, boolean isChecked) {
		if (isChecked) {
			((TextView) convertView.findViewById(R.id.tvMain))
					.setTextColor(Color.RED);
			((TextView) convertView.findViewById(R.id.tvSub))
					.setTextColor(Color.RED);
			((TextView) convertView.findViewById(R.id.tvRo))
					.setTextColor(Color.RED);
			((CheckBox) convertView.findViewById(R.id.checkBox))
					.setChecked(true);
		} else {
			((TextView) convertView.findViewById(R.id.tvMain))
					.setTextColor(convertView.getResources().getColor(
							R.color.TextColor));
			((TextView) convertView.findViewById(R.id.tvSub))
					.setTextColor(convertView.getResources().getColor(
							R.color.TextColor));
			((TextView) convertView.findViewById(R.id.tvRo))
					.setTextColor(convertView.getResources().getColor(
							R.color.TextColor));
			((CheckBox) convertView.findViewById(R.id.checkBox))
					.setChecked(false);
		}
	}
}
